#include "webinterface.h"
#include <QWebEnginePage>

WebInterface::WebInterface(QWebEnginePage* page, QObject* parent) : QObject(parent), page_(page) { }

ClientWebInterface::ClientWebInterface(QWebEnginePage* page, QObject* parent)
    : WebInterface(page, parent)
{
}

bool ClientWebInterface::invokeJsCallback(const QString& object_id, const QString& callback,
                                          const QString& args, bool keep)
{
    Q_UNUSED(object_id)
    if (page()) {
        // 回调代码
        QString code = QString("callbackFromClient('%1','%2',%3)").arg(callback, args).arg(keep);
        page()->runJavaScript(code);
        return true;
    }
    return false;
}

void ClientWebInterface::notifyClientEvent(const QString& event_name, const QString& args)
{
    if (page()) {
        QString code = QString("fireEventFromClient('%1','%2')").arg(event_name, args);
        page()->runJavaScript(code);
    }
}

void ClientWebInterface::invokeJsMethod(const QString& object_id, const QString& method,
                                        const QString& args, const QString& success_callback,
                                        const QString& failure_callback, const QString& sender_id)
{
    if (page()) {
        QString code = QString("if (typeof(%1.invokeFromClient) == 'function') { "
                               "%1.invokeFromClient('%2','%3','%4','%5','%6'); }")
                               .arg(object_id, method, args, success_callback, failure_callback,
                                    sender_id);
        page()->runJavaScript(code);
    }
}

MobileWebInterface::MobileWebInterface(QWebEnginePage* page, QObject* parent)
    : WebInterface(page, parent)
{
}

bool MobileWebInterface::invokeJsCallback(const QString& object_id, const QString& callback,
                                          const QString& args, bool keep)
{
    Q_UNUSED(object_id)
    Q_UNUSED(keep)
    if (page()) {
        // 回调代码
        QString code = QString("%1.invokeFromNative('%2','%3')").arg(object_id, callback, args);
        page()->runJavaScript(code);
        return true;
    }
    return false;
}

void MobileWebInterface::notifyClientEvent(const QString& event_name, const QString& args)
{
    if (page()) {
        QString code = QString("fireEventFromClient('%1','%2')").arg(event_name, args);
        page()->runJavaScript(code);
    }
}

void MobileWebInterface::invokeJsMethod(const QString& object_id, const QString& method,
                                        const QString& args, const QString& success_callback,
                                        const QString& failure_callback, const QString& sender_id)
{
    if (page()) {
        QString code = QString("if (typeof(%1.invokeFromClient) == 'function') { "
                               "%1.invokeFromClient('%2','%3','%4','%5','%6'); }")
                               .arg(object_id, method, args, success_callback, failure_callback,
                                    sender_id);
        page()->runJavaScript(code);
    }
}
